package com.oplink.webems.modules.cfg.entity;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;

/**
 * Configuration Card Entity
 * 
 * @author yizhang
 */
@Entity
@Table(name = "cfg_card")
@DynamicInsert
@DynamicUpdate
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class CfgCard {

	// ID
	private Long cardid;

	// 名称
	private String cardname;

	// 板卡类型
	private CfgCardType cardtype;

	@JSONField(serialize = false)
	List<CfgSlot> slotlist = Lists.newArrayList();

	@JSONField(serialize = false)
	List<CfgCardValue> cardVallist = Lists.newArrayList();

	private String comment;

	private String isEnable;

	private String type;

	// 还需预留生产信息、技术指标 ...

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getCardid() {
		return cardid;
	}
	
	@Transient
	public int getSlot(){
		int num=0;
		if ((this.getSlotlist()!=null)&&(this.getSlotlist().size()>0)){
			num = this.getSlotlist().get(0).getNum();
		}
		return num;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String isEnable() {
		return isEnable;
	}

	public void setEnable(String isEnable) {
		this.isEnable = isEnable;
	}

	public void setCardid(Long cardid) {
		this.cardid = cardid;
	}

	public String getCardname() {
		return cardname;
	}

	public void setCardname(String cardname) {
		this.cardname = cardname;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "cardtype")
	@NotFound(action = NotFoundAction.IGNORE)
	@JsonIgnore
	public CfgCardType getCardtype() {
		return cardtype;
	}

	public void setCardtype(CfgCardType cardtype) {
		this.cardtype = cardtype;
	}

	@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE }, fetch = FetchType.EAGER, mappedBy = "card")
	@NotFound(action = NotFoundAction.IGNORE)
	@JsonIgnore
	public List<CfgSlot> getSlotlist() {
		return slotlist;
	}

	public void setSlotlist(List<CfgSlot> slotlist) {
		this.slotlist = slotlist;
	}

	@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, fetch = FetchType.LAZY, mappedBy = "card")
	@NotFound(action = NotFoundAction.IGNORE)
	/* @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) */
	@JsonIgnore
	public List<CfgCardValue> getCardVallist() {
		return cardVallist;
	}

	public void setCardVallist(List<CfgCardValue> cardVallist) {
		this.cardVallist = cardVallist;
	}

	public void fromJson(JSONObject entity) {

	}
}